<?php
declare (strict_types = 1);

namespace app\controller;

use app\BaseController;
use app\common\model\Ad;
use app\common\model\Ipban;
use app\common\model\Iplog;
use app\common\model\Nav;
use think\facade\Cache;
use think\facade\Db;
use think\facade\Request;
use think\facade\Session;
use think\facade\View;


class Common extends BaseController
{
    protected  $user;
    protected  $params;
    public function __construct()
    {

        $this->iplog();
        //系统运行前相关操作
        $this->beforeSysStart();

        $this->user();
        View::assign("index_user",$this->user);

        //全局获取参数
        $this->params = request()->param();


        //最新动态
        $little_news = Db::name("news")->cache(900)->order("id DESC")->limit(5)->select();
        View::assign("little_news",$little_news);


        //最热文章 hot_list
        $hot_list = Db::name("content")->cache(900)
            ->whereYear('add_time')
            ->order("rel_hits DESC,hits DESC")
            ->where("model_id",1)
            ->limit(10)
            ->select();
        View::assign("hot_list",$hot_list);


        //友情链接
        $links = Db::name("link")->cache(1800)->where("status",1)->select();
        View::assign("links",$links);

        //导航的读取 及缓存
        if(!$nav = Cache::get("nav")){
            $nav = Nav::where("status",1)->order("ordid ASC,id ASC")->select()->toArray();
            $nav = unlimitforlayer($nav);
            Cache::set("nav",$nav);
        }
        View::assign("nav",$nav);

        //温馨通道

        if(!$ad_9 = Cache::get("ad_9")){
            $ad_9 = Ad::where("board_id",9)->where("status",1)
                ->select()->toArray();
            Cache::set("ad_9",$ad_9);
        }
        View::assign("ad_9",$ad_9);

        //热门标签调用
        $tag_list = Db::name("tags")
            ->where("status",1)
            ->order("hits DESC")
            ->limit(20)
            ->cache(true)
            ->select();
        View::assign("tag_list",$tag_list);

        //评论排行
        $clist = Db::name("content")
            ->where("status",1)
            ->order("comment DESC")
            ->limit(5)
            ->select();
        View::assign("clist",$clist);


    }

    //网站初始化获取用户相关信息
    public function user()
    {
        //1  从session中获取
        $user = Session::get("index_user");
        if($user){
            $this->user = $user;
            return true;
        }
        //2 从token中获取
        $token = \request()->header("token");
        if(!$token){
            $token = \request()->param("token");
        }
        //第一版小程序 传了个hello  埋了个坑
        if($token == 'hello'){
            return false;
        }
        if(!$token){
            return false;
        }
        $user = jwt_decode($token);

        if(!is_array($user)){
            $temp_user["id"] = $user->uid;
            $temp_user["headimgurl"] = $user->headimgurl;
            $temp_user["username"] = $user->username;
            $temp_user["nickname"] = $user->nickname;
            $temp_user["phone"] = $user->phone;
            $this->user = $temp_user;
            return true;
        }

        $this->user = $user;
        return true;


    }

    //系统开始前运行 做兼容处理
    public function beforeSysStart()
    {

        //去除网址中的index.php
        $request_url = \request()->url(true);
        if(strstr($request_url,"index.php")){
            $url = str_replace("index.php","",$request_url);
            header("Location:{$url}");
            exit;
        }

        //对旧版相关内容做兼容处理
        //http://v2.micuer.com/index.php?m=index&a=read&id=421
        $get = input('get.');
        if(is_array($get)){

            $m = isset($get["m"])?$get["m"]:'';
            $a = isset($get["a"])?$get["a"]:'';
            $id = isset($get["id"])?$get["id"]:'';

            if($m == 'index'  && $a == 'read'){
                $url = \think\facade\Request::domain().url('Article/read',['id'=>$id])->build();
                header("Location:{$url}");
                exit;
            }
            //http://v2.micuer.com/index.php?m=index&a=cate&id=21
            if($m === 'index'  && $a === 'cate'){
                $url = \think\facade\Request::domain().url('Index/index',['cid'=>$id])->build();
                header("Location:{$url}");
                exit;
            }
            //http://micuer.com/index.php?m=user&a=register
            if($m === 'user'  && $a === 'register'){
                $url = \think\facade\Request::domain().url('User/register')->build();
                header("Location:{$url}");
                exit;
            }

            if($m === 'user'  && $a === 'login'){
                $url = \think\facade\Request::domain().url('User/login')->build();
                header("Location:{$url}");
                exit;
            }

        }
    }

    //ip log记录 自动封禁 危险ip及访问 封禁思路   1h内访问次数达30次以上
    public function iplog()
    {

        $ip = request()->ip();
        $data["ip"] = $ip;
        $redis = Cache::store("redis");
        //无需限制的ip
        $redis->delete('111.21.172.242');
        $redis->delete('123.139.80.118');


        $count = $redis->get($ip);
        if(!$count){
            $redis->set($ip,1,3600);
        }
        if($count >= 120){
            $temp["ip"] = $ip;
            $temp["add_time"] = date("Y-m-d H:i:s",time());
            if(!Ipban::where("ip",$ip)->find()){
                Ipban::create($temp);
            }
            die("您的ip{$ip}被拉入了黑名单，请联管理员QQ571031767进行解封，或者等待1小时后自动解封");
        }else{
            $redis->inc($ip,1);
        }
        $data["accept"]  = Request::header('accept');
        $data["add_time"] = date("Y-m-d H:i:s",time());
        $data["request_url"] = request()->url();
        $data["user_agent"]  = Request::header('user-agent');
        $data["accept_encoding"]  = Request::header('accept-encoding');
        $res = Iplog::create($data);
        return true;
    }

    //显示错误信息
    public function err()
    {
        return \view("public/error");
    }



}
